package com.cognizant.ams.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cognizant.ams.bean.Lduser;
import com.cognizant.ams.bean.LduserExample;
import com.cognizant.ams.bean.LduserExample.Criteria;
import com.cognizant.ams.bean.Ldusertomenugrp;
import com.cognizant.ams.bean.LdusertomenugrpExample;
import com.cognizant.ams.dao.LduserMapper;
import com.cognizant.ams.dao.LdusertomenugrpMapper;

@Service
public class UserService {

	@Autowired
	private LduserMapper lduserMapper;
	
	@Autowired
	private LdusertomenugrpMapper tLdusertomenugrpMapper;

	public List<Map<String, Object>> getUser(String usercode,String username,String comcode) {

		
		if (username!=null && !username.equals("")) {
			if (username.equals("1")) {
				username="SUPER";
			}
			else {
				username="USER";
			}
		}
		if (usercode==null&&username==null&&comcode==null) {
			return null;
		}
		if (usercode.equals("") &&username.equals("")&&comcode.equals("")) {
			return null;
		}
		
		Map<String, Object>  param= new HashMap<String, Object>();
		Lduser lduser =new Lduser();
		lduser.setUsercode(usercode);
		lduser.setUsername(username);
		lduser.setComcode(comcode);
		param.put("param",lduser)	;

		 List<Map<String, Object>> ldusers = lduserMapper.getUser(param);
		 
		return  ldusers;
	}
	public List<Map<String, Object>> getUserByFenye(List<Map<String, Object>> list,String cur) {
		
		 	//本项目采用单页最大10条记录
			int totalCount =0;
			int current =Integer.parseInt(cur);
			if (list!=null) {
				totalCount=list.size();
			}
			
			int start=(current-1)*10;
			int end=current*10;
			System.out.println("宗条数为"+totalCount+"start"+start+"end条数为"+end);
			if (totalCount>=end) {
				list = list.subList(start,end );
			}
			else {
				System.out.println("超出");
				list = list.subList(start, totalCount);
			}
		return  list;
	}
	public void dealOp(String op,Lduser user) {
		LduserExample lduserExample = new LduserExample();
		Criteria criteria =lduserExample.createCriteria();
		criteria.andUsercodeEqualTo(user.getUsercode());
		if (op.equals("deleteUser")) {
			lduserMapper.deleteByExample(lduserExample);
		}
		if (op.equals("updateUser")) {
			Lduser lduser=new Lduser();
			lduser.setUsercode(user.getUsercode());
			lduserMapper.updateByExample(lduser, lduserExample);
		}
		if (op.equals("addUser")) {
			adduser(user);
		}
	}
	
	public int adduser(Lduser lduser) {
	
		if ("1".equals(lduser.getUsername())) {
			lduser.setUsername("super");
			lduser.setCertifyflag("0");
		} else {
			lduser.setUsername("user");
			lduser.setCertifyflag("1");
		}
		if ("1".equals(lduser.getUsername())) {
			lduser.setPassword("Password1$");
		}
		lduser.setOperator("007");
		lduser.setMakedate(new Date());
		lduser.setMaketime("00:00:00");
		Ldusertomenugrp tLdusertomenugrp=new Ldusertomenugrp();
		tLdusertomenugrp.setUsercode(lduser.getUsercode());
		tLdusertomenugrp.setMenugrpcode("audit");
		
		
		LdusertomenugrpExample tExample=new LdusertomenugrpExample();
		com.cognizant.ams.bean.LdusertomenugrpExample.Criteria criteria=tExample.createCriteria();
		criteria.andUsercodeEqualTo(lduser.getUsercode());
		criteria.andMenugrpcodeEqualTo("audit");
		List tList= tLdusertomenugrpMapper.selectByExample(tExample);
		if (null!=tList) {//用户已有菜单不添加
			if (tList.size()<=0) {
				tLdusertomenugrpMapper.insert(tLdusertomenugrp);//插入默认菜单组
			}
		}
		return lduserMapper.insert(lduser);//插入用户
	}

	/**
	 * 查询用户的所有角色
	 * @param usercode
	 * @return
	 */
	protected List<Ldusertomenugrp> getUserRole(String usercode){
		LdusertomenugrpExample tExample=new LdusertomenugrpExample();
		com.cognizant.ams.bean.LdusertomenugrpExample.Criteria criteria=tExample.createCriteria();
		criteria.andUsercodeEqualTo(usercode);
		return tLdusertomenugrpMapper.selectByExample(tExample);

	}

	/**
	 * 为用户分配角色
	 * @param record
	 * @return
	 */
	protected int AddRole2User(Ldusertomenugrp record){
		int result=tLdusertomenugrpMapper.insert(record);
		return  result;
	}

	/**
	 * 为用户移除某角色
	 * @param usercode
	 * @param rolecode
	 * @return
	 */
	protected int RemoveRoleFromUser(String usercode,String rolecode){
		LdusertomenugrpExample tExample=new LdusertomenugrpExample();
		com.cognizant.ams.bean.LdusertomenugrpExample.Criteria criteria=tExample.createCriteria();
		criteria.andUsercodeEqualTo(usercode);
		criteria.andMenugrpcodeEqualTo(rolecode);
		int result=tLdusertomenugrpMapper.deleteByExample(tExample);
		return result;
	}
}
